package com.tao.service;

import com.tao.mapper.CodingMapper;
import com.tao.pojo.Coding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@CacheConfig(cacheNames = "Coding")
public class CodingServiceImpl implements CodingService {
    @Autowired
    private CodingMapper codingMapper;

    @Autowired
    topicProductService topicProductService;

    @Autowired
    RedisTemplate redisTemplate;
    @Override
    @CacheEvict(key = "9527")

    public int deleteByPrimaryKey(Integer id) {
        return codingMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Coding selectByPrimaryKey(Integer id) {
        return codingMapper.selectByPrimaryKey(id);
    }

    @Override
    @CacheEvict(key = "9527")
    public int updateByPrimaryKeySelective(Coding record) {
        return codingMapper.updateByPrimaryKeySelective(record);
    }

    @CacheEvict(key = "9527")
    @Override
    public int updateByPrimaryKey(Coding record) {
        return codingMapper.updateByPrimaryKey(record);
    }

    @Override
    @Cacheable(value = "Coding",key ="9527" )
    public List<Coding> selectAll() {
        return codingMapper.selectAll();
    }

    @Override
    @CacheEvict(key = "9527")
    public int insertCoding(Coding coding) {

        System.out.println("redisHash:"+coding.toString());
        redisTemplate.opsForHash().put("codingRedisTest",coding.getUserid().toString(),coding);
        topicProductService.makeCoding(coding);
        return codingMapper.insertCoding(coding);
    }
}
